package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.QuestionItem;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/store/questionItem")
public class QuestionItemServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String operation = req.getParameter("operation");
        if ("list".equals(operation)) {
            this.list(req, resp);
        } else if ("save".equals(operation)) {
            this.save(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            edit(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        QuestionItem questionItem = BeanUtil.fillBean(req, QuestionItem.class);
        // QuestionItemService questionItemService=new QuestionItemServiceImpl();
        questionItemService.delete(questionItem);
        resp.sendRedirect(req.getContextPath() + "/store/questionItem?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        // QuestionItemService questionItemService=new QuestionItemServiceImpl();
        QuestionItem questionItem = questionItemService.findById(id);
        System.out.println(questionItem);
        req.setAttribute("questionItem", questionItem);
        req.getRequestDispatcher("/WEB-INF/pages/store/questionItem/update.jsp").forward(req, resp);

    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询要修改的数据
        QuestionItem questionItem = BeanUtil.fillBean(req, QuestionItem.class, "yyyy-MM-dd");

        //QuestionItemService questionItemService=new QuestionItemServiceImpl();
        questionItemService.update(questionItem);
        resp.sendRedirect(req.getContextPath() + "/store/questionItem?operation=list");


    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        QuestionItem questionItem = BeanUtil.fillBean(req, QuestionItem.class, "yyyy-MM-dd");
        // QuestionItemService questionItemService=new QuestionItemServiceImpl();
        questionItemService.save(questionItem);
        list(req, resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String questionId = req.getParameter("questionId");
        //进入list页面是添加对应的问题id，为添加操作使用
        req.setAttribute("questionId", questionId);
        PageInfo all = questionItemService.findAll(questionId, 1, 100);
        //将数据保存到指定的位置
        req.setAttribute("page", all);
        req.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list2.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
